#import "ALUtils.h"
#import <UserNotifications/UserNotifications.h>
@implementation ALUtils


+(NSString*)currentDateFormat:(NSString*)formatStr{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:formatStr];
    NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];
    return currentDateStr;
}


+ (void) getUserNotificationSettingsWithCompletionHandler:(void (^)(ALNotificationType types))handler {
    if (DEVICE_IOS_VERSION >= 10.0f) {
        [[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
            ALNotificationType types = ALNotificationTypeNone;
            if (settings != nil
                && settings.authorizationStatus == UNAuthorizationStatusAuthorized) {
                if (settings.alertSetting == UNNotificationSettingEnabled) {
                    types |= ALNotificationTypeAlert;
                }
                if (settings.badgeSetting == UNNotificationSettingEnabled) {
                    types |= ALNotificationTypeBadge;
                }
                if (settings.soundSetting == UNNotificationSettingEnabled) {
                    types |= ALNotificationTypeSound;
                }
            } // end if
            handler(types);
        }];
        
    } else if (DEVICE_IOS_VERSION >= 8.0f) {
        UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
        UIUserNotificationType systemTypes = setting.types;
        ALNotificationType types = ALNotificationTypeNone;
        if ((systemTypes & UIUserNotificationTypeAlert) != 0) {
            types |= ALNotificationTypeAlert;
        }
        if ((systemTypes & UIUserNotificationTypeBadge) != 0) {
            types |= ALNotificationTypeBadge;
        }
        if ((systemTypes & UIUserNotificationTypeSound) != 0) {
            types |= ALNotificationTypeSound;
        }
        handler(types);
        
    } else {
        //iOS 7
        UIRemoteNotificationType systemTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
        ALNotificationType types = ALNotificationTypeNone;
        if ((systemTypes & UIRemoteNotificationTypeAlert) != 0) {
            types |= ALNotificationTypeAlert;
        }
        if ((systemTypes & UIRemoteNotificationTypeBadge) != 0) {
            types |= ALNotificationTypeBadge;
        }
        if ((systemTypes & UIRemoteNotificationTypeSound) != 0) {
            types |= ALNotificationTypeSound;
        }
        handler(types);
    }
}


+ (BOOL)isFirstLaunch{
    NSDictionary *bundleInfo = [NSBundle mainBundle].infoDictionary;
    NSString *nativeVersion = [bundleInfo valueForKey:@"CFBundleShortVersionString"];
    NSString *firstLaunchMark = [NSString stringWithFormat:@"app_first_launch:%@", nativeVersion];
    
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    NSString *app_first_launch = [ud objectForKey:@"app_first_launch"];
    
    // 判断是否是新装启动
    if (!app_first_launch) {
        [[NSUserDefaults standardUserDefaults] setObject:firstLaunchMark forKey:@"app_first_launch"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        return YES;
    }
    
    // 判断是否是覆盖安装
    if (![app_first_launch isEqualToString:firstLaunchMark]) {
        [[NSUserDefaults standardUserDefaults] setObject:firstLaunchMark forKey:@"app_first_launch"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        return YES;
    }
    
    return NO;
}

//获取自定义，自己创建的bundle就是在主bundle下建了个子bundle
+(NSBundle*)bundleByName:(NSString*)bundelName{
    NSString *bundlePath = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:bundelName];
    return [NSBundle bundleWithPath:bundlePath];
}


+ (void)performBlockOnMainThread:(DISPATCH_NOESCAPE dispatch_block_t)block {
    if (NSThread.isMainThread) {
        block();
    } else {
        dispatch_sync(dispatch_get_main_queue(), block);
    }
    
}

+ (BOOL)isSameQueue:(dispatch_queue_t)queue {
    return strcmp(dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL), dispatch_queue_get_label(queue)) == 0;
}

@end
